/**
 * Created by shopnc on 2016/9/1.
 */
//定义表格
var dtGridColumns = [{
    id: 'title',
    title: '回复主题',
    type: 'string',
    headerClass: 'text-left',
    columnClass: 'text-left width-200',
    fastSort: false,
    fastQuery: true,
    fastQueryType: 'eq'
}, {
    id: 'code',
    title: '点击标识码',
    type: 'string',
    headerClass: 'text-left',
    columnClass: 'text-left width-200',
    fastSort: false,
    fastQuery: true,
    fastQueryType: 'eq',
    hideType: 'xs|sm'
}, {
    id: 'note',
    title: '回复说明',
    type: 'string',
    headerClass: 'text-left',
    columnClass: 'text-left',
    fastSort: false,
    fastQuery: true,
    fastQueryType: 'eq',
    hideType: 'xs|sm|md'
}, {
    id: 'type',
    title: '回复类型',
    type: 'string',
    headerClass: 'text-left',
    columnClass: 'text-left width-100',
    fastSort: false,
    fastQuery: true,
    fastQueryType: 'eq',
    hideType: 'xs|sm',
    codeTable: {
        1: $lang.replyType.content,
        2: $lang.replyType.picture
    }
}, {
    id: 'createTime',
    title: '添加时间',
    type: 'string',
    headerClass: 'text-left',
    columnClass: 'text-left width-200',
    fastSort: false,
    fastQuery: true,
    fastQueryType: 'eq',
    hideType: 'xs|sm|md'
}, {
    id: 'operation',
    title: '管理操作',
    type: 'string',
    columnClass: 'text-center width-200',
    fastSort: false,
    resolution: function(value, record, column, grid, dataNo, columnNo) {
        var content = '';
        content +=
            '<a data-toggle="modal" href="#editModal" class="btn btn-primary btn-sm m-r-10" data-no="' + dataNo + '" ><i class="fa fa-edit"></i>&nbsp;编辑&nbsp;</a>' + '</div>';

        content += "<a href='javascript:;' class='btn btn-sm btn-danger' onclick='reason.del(" + record.id + ")'><i class='fa fa-trash-o'></i>删除</a>";

        return content;
    }
}];
var dtGridOption = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + 'clickreply/json/list',
    exportFileName: '点击回复素材列表',
    columns: dtGridColumns,
    gridContainer: 'dtGridContainer',
    toolbarContainer: 'dtGridToolBarContainer',
    tools: '',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    onGridComplete: function(grid) {

    }
};
var grid = $.fn.DtGrid.init(dtGridOption);
grid.sortParameter.sortType = 1;

var reason = function() {


    /**
     * 刷新addmodal 上的图片
     */
    function _refreshAddModalImage() {
        $("#addFormPicImageImg").jqthumb({
            width: 180,
            height: 100 ,
             after: function (imgObj) {
                imgObj.css('opacity', 0).animate({opacity: 1}, 1500);
            }
        });
    }
    /**
     * 刷新编辑框上的图片
     */
    function _refreshEditModalImage() {
        $("#editFormPicImageImg").jqthumb({
            width: 180,
            height: 100 , 
             after: function (imgObj) {
                imgObj.css('opacity', 0).animate({opacity: 1}, 1500);
            }

        });
    }

    /**
     * 绑定事件
     * @private
     */
    function _bindEvent() {
        //模糊查询
        $("#customSearch").click(function() {
            grid.fastQueryParameters = new Object();
            grid.fastQueryParameters['eq_delFlag'] = 1;
            grid.fastQueryParameters['lk_title'] = $('#keyword').val();
            grid.pager.startRecord = 0;
            grid.pager.nowPage = 1;
            grid.pager.recordCount = -1;
            grid.pager.pageCount = -1;
            grid.refresh(true);
        });
        //添加微信事件时显示时
        $('#addModal').on('show.bs.modal', function(event) {
            $("#addForm").psly().reset();
            $(".alert-danger").remove();
            _refreshAddModalImage();
        });
        //编辑微信事件时显示时
        $('#editModal').on('show.bs.modal', function(event) {
                var
                    button = $(event.relatedTarget),
                    datano = button.data('no'),
                    gridData = grid.sortOriginalDatas[datano],
                    editForm = $("#editForm");
                $("#editCrId").val(gridData.id);
                $("#editCrTitle").val(gridData.title);
                $("#editCrNote").val(gridData.note);
                $("#editCrCode").val(gridData.code);
                //文字
                if (gridData.type == 1) {
                    $("#editCrContent").val(gridData.content);
                    $('input[name="editType"][value="1"]').prop("checked", true);
                    $('input[name="editType"][value="2"]').prop("checked", false);
                    $("#editTextFields").show();
                    $("#editPicFields").hide();
                    //图文
                } else {
                    $("#editCrContent").val('');
                    $('input[name="editType"][value="2"]').prop("checked", true);
                    $('input[name="editType"][value="1"]').prop("checked", false);
                    $("#editTextFields").hide();
                    $("#editPicFields").show();
                    $.post(ncGlobal.adminRoot + "clickreply/json/getcontent", {
                        content: gridData.content
                    }, function(data) {
                        if (data.code == 200 && data.data != null) {
                            $('#editFormPicImage').val(data.data.picurl);
                            $('#editFormPicImageImg').attr('src', ncGlobal.fileRoot + data.data.picurl);
                            $("#editPicTitle").val(data.data.title);
                            $("#editPicDesc").val(data.data.desc);
                            $("#editPicClickUrl").val(data.data.url);
                             _refreshEditModalImage();  
                        }

                    });
                }
                 _refreshEditModalImage();  
                editForm.psly().reset();
                $(".alert-danger").remove();
            });
            //添加的form 成功返回时
        $("#addForm").on("nc.formSubmit.success", function() {
                $("#addReasonInfo").val('');
                $("#addReasonSort").val('');
                $("#addForm").psly().reset();
                $(".alert-danger").remove()
            })
            //控制文字、图文输入框切换
        $("#textFields").hide();

        $('input[name="type"]').change(function() {
            if ($('input[name="type"]:checked').val() == "1") {
                $("#textFields").show();
                $("#picFields").hide();
            } else {
                $("#textFields").hide();
                $("#picFields").show();
            }
        });

        $('input[name="editType"]').change(function() {
            if ($('input[name="editType"]:checked').val() == "1") {
                $("#editTextFields").show();
                $("#editPicFields").hide();
            } else {
                $("#editTextFields").hide();
                $("#editPicFields").show();
            }
        });

        //图片上传
        $("#addFormFile").fileupload({
            dataType: 'json',
            url: ncGlobal.adminRoot + "file/upload",
            done: function(e, data) {
                if (data.result.code == 200) {
                    $('#addFormPicImage').val(data.result.data);
                    $('#addFormPicImageImg').attr('src', ncGlobal.fileRoot + data.result.data);
                    //图片同比例缩放-新增
                    _refreshAddModalImage();
                } else {
                    $.ncAlert({
                        closeButtonText: "关闭",
                        autoCloseTime: 3,
                        content: data.result.message
                    })
                }
            }
        });
        /**
         * 上传插件绑定
         */
        $("#editFormFile").fileupload({
            dataType: 'json',
            url: ncGlobal.adminRoot + "file/upload",
            done: function(e, data) {
                if (data.result.code == 200) {
                    $('#editFormPicImage').val(data.result.data);
                    $('#editFormPicImageImg').attr('src', ncGlobal.fileRoot + data.result.data);
                    //图片同比例缩放-编辑
                    _refreshEditModalImage();
                } else {
                    $.ncAlert({
                        closeButtonText: "关闭",
                        autoCloseTime: 3,
                        content: data.result.message
                    })
                }
            }
        });
    }

    //外部可调用
    return {
        init: function() {
            _bindEvent();
        },
        //删除
        del: function(id) {

            $.ncConfirm({
                url: ncGlobal.adminRoot + "clickreply/json/del",
                data: {
                    id: id
                }
            });

        }
    }
}()
$(function() {
    grid.load();
    reason.init();
});